//
//  CouponView.m
//  Findif
//
//  Created by Cicel Chen on 2018/10/19.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "CouponView.h"
#import "CouponHeaderView.h"
#import "CouponTableViewCell.h"
#import "SureUserCouponModel.h"

@interface CouponView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray <SureUserCouponModel *> *userModel;
@property (nonatomic, weak)UILabel * showLabel;


@end

@implementation CouponView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        
        [self loadData];
        [self setupUI];
    }
    
    return self;
}


- (void)loadData{
    
    [[ZRNetworkTool sharedTool] getUserCouponsResultWithMerchants_id:@"" WithBlock:^(BOOL success, id responseData) {

        if (success) {

            self.userModel = [NSArray yy_modelArrayWithClass:[SureUserCouponModel class] json:responseData[@"data"]];
        }
    }];
}

- (void)setUserModel:(NSArray<SureUserCouponModel *> *)userModel{
    
    _userModel = userModel;
    
    [self.tableView reloadData];
}


- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ZRWhiteColor;
    tableView.rowHeight = 105 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    CouponHeaderView * headerView =  [[CouponHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 45 * SCREENH6)];
    // 设置header
    tableView.tableHeaderView = headerView;
    //    self.tableView.bounces = NO;
    //    self.tableView.estimatedRowHeight = 150 * SCREENH6;//估算高度
    //    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[CouponTableViewCell class] forCellReuseIdentifier:@"CouponListCell"];
    
    [self addSubview:self.tableView];
    
    
    UILabel * showLabel = [UILabel new];
    
    showLabel.text = @"您没有优惠券哦,赶快去领取吧";
    showLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    showLabel.textColor = ZRBlackColor;
    showLabel.hidden = YES;
    
    self.showLabel = showLabel;
    
    [self addSubview:self.showLabel];
    
}

- (void)layoutSubviews{
    
    [self.showLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.centerY.mas_equalTo(self);
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.userModel.count == 0) {
        
        self.showLabel.hidden = NO;
    }else{
        
        self.showLabel.hidden = YES;
    }
    return self.userModel.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CouponTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CouponListCell" forIndexPath:indexPath];
    
    cell.userModel = self.userModel[indexPath.row];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SureUserCouponModel * model = self.userModel[indexPath.row];
    
    NSString * shop_id = [NSString stringWithFormat:@"%@",model.merchants_id];
    
    if (self.homeCouponViewDelegate && [self.homeCouponViewDelegate respondsToSelector:@selector(clickUseButtonWithShop_id:)])
    {
        [self.homeCouponViewDelegate clickUseButtonWithShop_id:shop_id];
    }
}



@end
